//
//  daibanViewController.m
//  tongxiangOASystem
//
//  Created by michael on 2018/3/29.
//  Copyright © 2018年 czd. All rights reserved.
//

#import "daibanViewController.h"

@interface daibanViewController ()<UIScrollViewDelegate,SectionChooseVCDelegate>
//底部滚动ScrollView
@property (nonatomic, strong) UIScrollView *contentScrollView;

@property(nonatomic,strong)SectionChooseView *sectionChooseView;


@end

@implementation daibanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.title = @"待办";
    self.view.backgroundColor = [UIColor colorWithHexString:@"f5f5f5"];
    // 首次进入加载第一个界面通知
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showFirstVC) name:@"ABC" object:nil];
    
    //添加所有子控制器
    [self setupChildViewController];
    
    //初始化UIScrollView
    [self setupUIScrollView];
    
}

- (void)showFirstVC {
    
    [self showVc:0];
}


- (void)setupUIScrollView {
    
    // 创建底部滚动视图
    self.contentScrollView = [[UIScrollView alloc] init];
    _contentScrollView.frame = CGRectMake(0, 0, KUIScreenWidth, KUIScreenHeight-kTopHeight-44);
//        _contentScrollView.frame = CGRectMake(0, 44, KUIScreenWidth, KUIScreenHeight-kTopHeight);

    _contentScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    _contentScrollView.backgroundColor = [UIColor clearColor];
    // 开启分页
    _contentScrollView.pagingEnabled = YES;
    // 没有弹簧效果
    _contentScrollView.bounces = NO;
    // 隐藏水平滚动条
    _contentScrollView.showsHorizontalScrollIndicator = NO;
    // 设置代理
    _contentScrollView.delegate = self;
    [self.view addSubview:_contentScrollView];
    
    self.sectionChooseView = [[SectionChooseView alloc] initWithFrame:CGRectMake(0, _contentScrollView.bottom, self.view.frame.size.width, 44) titleArray:@[@"待办", @"已办"]];
//        self.sectionChooseView = [[SectionChooseView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44) titleArray:@[@"苍老师", @"一本道"]];

    self.sectionChooseView.selectIndex = 0;
    self.sectionChooseView.delegate = self;
//    self.sectionChooseView.normalBackgroundColor = [UIColor whiteColor];
//    self.sectionChooseView.selectBackgroundColor = [UIColor orangeColor];;
//    self.sectionChooseView.titleNormalColor = [UIColor orangeColor];
//    self.sectionChooseView.titleSelectColor = [UIColor whiteColor];
    self.sectionChooseView.normalTitleFont = 14;
    self.sectionChooseView.selectTitleFont = 16;
    [self.view addSubview:self.sectionChooseView];
    
}

#pragma mark -添加所有子控制器

-(void)setupChildViewController {
    
    FirstViewController *voucherUnusedVC = [[FirstViewController alloc] init];
        voucherUnusedVC.selectRowBlock = ^(NSString *rowTitle) {
self.title = rowTitle;
};
    [self addChildViewController:voucherUnusedVC];
    
    
    SecondViewController *voucherUsedVC = [[SecondViewController alloc] init];
     voucherUsedVC.selectRowBlock = ^(NSString *rowTitle) {
self.title = rowTitle;
};
    [self addChildViewController:voucherUsedVC];
    
}

#pragma mark -SMCustomSegmentDelegate

- (void)SectionSelectIndex:(NSInteger)selectIndex {
    
    NSLog(@"---------%ld",(long)selectIndex);
    
    // 1 计算滚动的位置
    CGFloat offsetX = selectIndex * self.view.frame.size.width;
    self.contentScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 2.给对应位置添加对应子控制器
    [self showVc:selectIndex];
}

#pragma mark -显示控制器的view
/**
 *  显示控制器的view
 *
 *  @param index 选择第几个
 *
 */
- (void)showVc:(NSInteger)index {
SectionChooseView *view = [SectionChooseView alloc];

    view.buttonblock = ^{
    NSLog(@"111");
};
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
//    if (vc.isViewLoaded) return;
    for(UIView *subv in [self.contentScrollView subviews])
{
[subv removeFromSuperview];
}
    [self.contentScrollView addSubview:vc.view];
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.view.frame.size.height);
}

#pragma mark -UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    // 1.添加子控制器view
    [self showVc:index];
    
    // 2.把对应的标题选中
    self.sectionChooseView.selectIndex = index;
    
    
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

